<template>
  <div>
    <div class="header">
      <div class="title">精品歌单 <i class="el-icon-question"></i></div>

      <div class="tags_popover">
        <el-popover
          placement="left-start"
          width="660"
          v-model="tagsListVisible"
        >
          <div class="tags_categoryDetail">
            <div v-for="(tag, key) in allTagList" :key="key">
              <span
                :class="tagActive.tagId == tag.id ? 'tag_active' : ''"
                @click="selectTag(tag.id, tag.name)"
              >
                {{ tag.name }}
              </span>
            </div>
          </div>

          <el-button class="btn" round v-remove-focus slot="reference">
            <i class="el-icon-goblet-full"></i>
            {{ tagActive.tagName }}
          </el-button>
        </el-popover>
      </div>
    </div>

    <!-- 歌单列表 -->
    <div class="musicList">
      <div v-for="item in highQualityList" :key="item.id">
        <div class="left_img" @dblclick="toSonglistPage(item.id)">
          <el-image
            style="width: 160px; height: 160px"
            :src="item.picUrl"
            fit="fill"
          ></el-image>
          <div class="playCount">
            <i class="el-icon-caret-right"></i>
            {{ item.playCount }}
          </div>
          <div class="play">
            <i class="el-icon-caret-right"></i>
          </div>
        </div>

        <div class="desc">
          <div class="name">{{ item.name }}</div>
          <div class="nickname">by {{ item.nickname }}</div>
          <div class="copywriter">{{ item.copywriter }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'

export default {
  props: ['tagId', 'tagName'],
  data() {
    return {
      // 精品歌单
      highQualityList: [],
      // 歌单标签列表显示与否
      tagsListVisible: false,
      // 展示的标签
      tagActive: {
        tagId: '',
        tagName: '',
      },
      // 标签列表
      allTagList: [],
    }
  },
  created() {
    this.getTagsAboutInfo()
    this.getHighQualityTags()
  },
  watch: {
    'tagActive.tagId': function (newVal, oldVal) {
      this.getHighQualityList()
    },
  },
  methods: {
    ...mapMutations(['changePath', 'changeIsActive', 'changeSonglistId']),

    // 获取要展示的标签标签信息
    getTagsAboutInfo() {
      if (this.tagId != '') {
        const tagActive = {
          tagId: this.tagId,
          tagName: this.tagName,
        }
        window.sessionStorage.setItem(
          'HighQualityList_tagInfo',
          JSON.stringify(tagActive)
        )

        this.tagActive.tagId = this.tagId
        this.tagActive.tagName = this.tagName
        return
      }

      let tagInfo =
        window.sessionStorage.getItem('HighQualityList_tagInfo') || ''

      // console.log(tagInfo)
      // console.log(typeof this.tagId)

      tagInfo = JSON.parse(tagInfo)
      this.tagActive.tagId = tagInfo.tagId
      this.tagActive.tagName = tagInfo.tagName
    },
    // 获取精品歌单标签列表
    async getHighQualityList() {
      const { data: res } = await this.$http({
        url: `/top/playlist/highquality?cat='${this.tagActive.tagName}'&limit=20`,
        withCredentials: true, //关键
      })

      // console.log(res)
      let tmepListDatas = []
      res.playlists.map((item) => {
        let listObj = {}

        listObj.id = item.id
        listObj.name = item.name
        listObj.picUrl = item.coverImgUrl
        listObj.nickname = item.creator.nickname
        listObj.playCount = item.playCount
        listObj.copywriter = item.copywriter

        return tmepListDatas.push(listObj)
      })

      this.highQualityList = tmepListDatas
    },
    // 获取精品歌单标签列表
    async getHighQualityTags() {
      const { data: res } = await this.$http({
        url: `/playlist/highquality/tags`,
        withCredentials: true, //关键
      })
      // console.log(res)

      let tmepListDatas = []
      res.tags.map((item) => {
        let listObj = {}

        listObj.id = item.id
        listObj.name = item.name

        return tmepListDatas.push(listObj)
      })
      this.allTagList = tmepListDatas
      // console.log(this.allTagList)
    },
    // 选择标签
    selectTag(tagId, tagName) {
      const tagActive = {
        tagId: tagId,
        tagName: tagName,
      }
      window.sessionStorage.setItem(
        'HighQualityList_tagInfo',
        JSON.stringify(tagActive)
      )

      this.tagActive.tagId = tagId
      this.tagActive.tagName = tagName
    },
    // 传递 歌单数据，并跳转到歌单页面
    toSonglistPage(songlistId) {
      const info = {
        path: '/view/songlist',
        isActive: songlistId,
        songlistId: songlistId,
      }

      this.changePath(info.path)
      this.changeIsActive(info.isActive)
      this.changeSonglistId(info.songlistId)

      this.$emit('getSonglistId')
    },
  },
}
</script>

<style lang="less" scoped>
// 弹出框样式
.tags_categoryDetail {
  display: flex;
  justify-content: space-between;
  width: 660px;
  display: flex;
  flex-wrap: wrap;
  padding-top: 10px;
  padding-bottom: 5px;

  > div {
    width: 120px;
    padding-bottom: 15px;
    > span {
      padding: 6px 12px;
      font-size: 16px;
      cursor: pointer;
    }
    > span:hover {
      color: #ec4141;
    }
  }
}
.tag_active {
  background-color: #fdf5f5;
  color: #ec4141;
  border-radius: 30px;
}

.header {
  padding: 16px 0;
  display: flex;
  justify-content: space-between;
  > .title {
    font-size: 20px;
    font-weight: bold;
    padding-left: 12px;
    > .el-icon-question {
      position: relative;
      left: -4px;
      color: #ac9f9f;
    }
  }
  > .tags_popover {
    .btn {
      padding: 6px 12px;
      font-size: 17px;
    }
  }
}

.musicList {
  display: flex;
  flex-wrap: wrap;
  > div {
    padding: 0px 12px 20px 12px;
    position: relative;
    cursor: pointer;
    display: flex;
    flex-wrap: wrap;

    > .left_img {
      position: relative;
      .el-image {
        border-radius: 6px;
      }
      > .playCount {
        position: absolute;
        right: 10px;
        top: 2px;
        color: white;
        font-size: 13.6px;
      }
      > .play {
        position: absolute;
        right: 12px;
        bottom: 16px;
        border-radius: 50%;
        width: 32px;
        height: 31px;
        background-color: white;
        transition: 1s;
        opacity: 0;
        > .el-icon-caret-right {
          font-size: 28px;
          color: #ec4141;
          position: relative;
          left: 3px;
          top: 1px;
        }
      }
    }
    > .left_img:hover {
      transition: 1s;
      > .play {
        opacity: 1;
      }
    }
    > .desc {
      width: 250px;
      padding-left: 10px;
      > .name {
        width: 240px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        padding-top: 30px;
      }
      > .nickname {
        padding-top: 16px;
        font-size: 14px;
        color: #a3a3a3;
      }
      > .copywriter {
        padding-top: 20px;
        font-size: 14px;
        color: #cfcfcf;
      }
    }

    // > .name {
    //   width: 160px;
    //   font-size: 15px;
    //   padding-top: 4px;
    //   overflow: hidden;
    //   text-overflow: ellipsis;
    //   //将对象作为弹性伸缩盒子模型显示。
    //   display: -webkit-box;
    //   //从上到下垂直排列子元素（设置伸缩盒子的子元素排列方式）
    //   -webkit-box-orient: vertical;
    //   //这个属性不是css的规范属性，需要组合上面两个属性，表示显示的行数。
    //   -webkit-line-clamp: 2;
    // }
  }
}
</style>